package com.xizi.exception;

import com.xizi.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice  //处理全局异常注解
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)  //处理Controller层抛出的异常
    @ResponseBody //返回请求体
    public Result error(Exception e){
            e.printStackTrace();
        return Result.fail();
    }

    /**
    *   自定义异常处理方法
    * @param e
    *   @return
    */
    @ExceptionHandler(HospitalException.class) //对于自定义异常进行处理
    @ResponseBody
    public Result error(HospitalException e){
    return Result.build(e.getCode(), e.getMessage());
        }
    }